/*****************************************************************************
 * Copyright (C) 2009 Gwathuirim
 *
 * This is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 *
 * This is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Public License for more details
 *
 * To obtain a copy of the GNU General Public License, please see
 * <http://www.gnu.org.licenses/>
 *****************************************************************************/

// ==UserScript==
// @name           Imperion - ResearchHelper
// @namespace      http://code.google.com/p/imperion-gm/
// @include        http://*.imperion.*/researchCenter/*
// @require		   http://stevenlevithan.com/assets/misc/date.format.js
// ==/UserScript==

/* language entries */
var language = new Object();

/* German */
/* For the *.imperion.de servers */
language['de'] = new Object();
language['de']['regExpr_insufficientPoints'] = "unzureichende Punkte";
language['de']['regExpr_decimalSeperator'] = ",";
language['de']['regExpr_thousandSeperator'] = ".";
language['de']['format_insufficientPointsReplacementDate'] = 'dd.mm.yy "-" HH:MM "Uhr"';

/* English */
/* For the *.imperion.org servers */
language['org'] = new Object();
language['org']['regExpr_insufficientPoints'] = "not enough points";
language['org']['format_insufficientPointsReplacementDate'] = 'mm/dd/yy "-" h:MM TT';
language['org']['regExpr_decimalSeperator'] = ".";
language['org']['regExpr_thousandSeperator'] = ",";

/* Determine TLD for language support */
var languageKey = window.location.hostname;
languageKey = languageKey.split(".");
languageKey = languageKey[languageKey.length - 1];

var currentLanguage = language[languageKey];

/* Some constants */
var categoryIds = new Object();
categoryIds['Ingenieurswesen'] = 1;
categoryIds['PlanetareVerteidigung'] = 2;
categoryIds['MilitaerischeRaumfahrt'] = 3;
categoryIds['ZivileRaumfahrt'] = 4;
categoryIds['Energietechnik'] = 5;

var researchCategoryId = 'researchCategory';
var researchPointProduction = 'researchPointProduction';
var researchPoints = 'researchPoints';

/**
 * Collects informations about the category. This includes the per day production and the points in the category
 */
function getCategoryInformations(categoryId) {
	var resultInformations = new Object();
	
	var tbody = document.getElementById("categorie_" + categoryId);
	tbody = tbody.parentNode.parentNode.parentNode;
	
	resultInformations[researchCategoryId] = categoryId;
	resultInformations[researchPointProduction] = parseFloat(tbody
		.getElementsByTagName("tr")[2]
		.getElementsByTagName("td")[0]
		.innerHTML.replace(currentLanguage["regExpr_thousandSeperator"], "").replace(currentLanguage["regExpr_decimalSeperator"], ".").match(/\d+(?:.\d+)?/));
	resultInformations[researchPoints] = parseFloat(tbody
		.getElementsByTagName("tr")[0]
		.getElementsByTagName("td")[2]
		.getElementsByTagName("div")[0]
		.innerHTML.replace(currentLanguage["regExpr_thousandSeperator"], "").replace(currentLanguage["regExpr_decimalSeperator"], ".").match(/\d+(?:.\d+)?/));
		
	return resultInformations;
}

/**
 * Calculates and published the date for researching
 */
function calculateAndPublishResearches(researchCategory) {
	var tbody = document.getElementById("categorie_" + researchCategory[researchCategoryId] + "_slide");
	tbody = tbody.getElementsByTagName("table")[0].getElementsByTagName("tbody")[0];
	
	var tableRows = tbody.getElementsByTagName("tr");
	for (var i = 2; i < tableRows.length; i = i + 2) {
		var currentRow = tableRows[i];
		
		if (currentRow.innerHTML.match(currentLanguage['regExpr_insufficientPoints']) != null) {
			//now calculate the estimated time
			var neededPoints = parseInt(currentRow.getElementsByTagName("td")[2].innerHTML.match(/\d+/));
			var pointsLeft = neededPoints - researchCategory[researchPoints];
			
			var timeLeft = pointsLeft / researchCategory[researchPointProduction];
			var timeLeft = timeLeft * 24 * 60 * 60 * 1000; //timeLeft in milliseconds
			
			var finishDate = new Date();
			finishDate.setMilliseconds(finishDate.getMilliseconds() + timeLeft);
			currentRow.getElementsByTagName("td")[3].getElementsByTagName("a")[0].childNodes[2].nodeValue = finishDate.format(currentLanguage['format_insufficientPointsReplacementDate']);
		}
	}
}

var researchCategories = new Array();
researchCategories.push(getCategoryInformations(categoryIds['Ingenieurswesen']));
researchCategories.push(getCategoryInformations(categoryIds['PlanetareVerteidigung']));
researchCategories.push(getCategoryInformations(categoryIds['MilitaerischeRaumfahrt']));
researchCategories.push(getCategoryInformations(categoryIds['ZivileRaumfahrt']));
researchCategories.push(getCategoryInformations(categoryIds['Energietechnik']));

for (var i = 0; i < researchCategories.length; i++) {
	calculateAndPublishResearches(researchCategories[i]);
}